#!/usr/bin/env python
# $Id: flowd.py 83306ac00e39 2013/03/03 02:04:53 pgurumur $
# Copyright (c) 2012 Prabhu Gurumurthy <pgurumur@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# File flowd.py generated on Tuesday December 04, 2012 @16:29:01 PST
#
__version__  = "$Revision: 83306ac00e39 $"
__author__   = "$Author: pgurumur $"
__modified__ = "$Date: 2013-03-03 02:04:53Z $"

import optparse
import os
import sys

from venya.lib import cmdparser, exception, ip, stdlib
import flowlib, server

class flow(cmdparser.GetOpt):
   option_list = (
         optparse.make_option("-p", "--port", type = "int",
            help = "Flow daemon port to listen", default = 9995, dest = "port"),
   ) + cmdparser.GetOpt.option_list

   usage = "[ options ]"
   help = "Something to write about!"

   def __init__(self, **kwargs):
      super(flow, self).__init__(**kwargs)

   @cmdparser.getopts
   def handle(self):
      port = 0
      if self.options.port:
         port = self.options.port

      try:
         self._server = server.FlowServer(('', self.options.port),
               server.FlowRequestHandler)
         print("Starting flow server on port: %d" %self.options.port)
         self._server.serve_forever()
      except KeyboardInterrupt:
         print("stopping flow server on port: %d" %self.options.port)

if __name__ == "__main__":
   daemon = None
   try:
      daemon = flow(version = 0.1, args = sys.argv)
      daemon.handle()
   except exception.GetOptError as err:
      print(err)
      sys.exit(1)
